package com.comparyinfo.servlet;
import com.comparyinfo.entity.Xinwen;
import com.comparyinfo.service.XinwenService;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.*;

@WebServlet(name = "XinwenServlet",urlPatterns = "/XinwenServlet")
public class XinwenServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String method=request.getParameter("method");
        request.setCharacterEncoding("UTF-8");
        XinwenService service=new XinwenService();
        if(method.equals("getAll")){//列表
            Xinwen obj=new Xinwen();
            //获取列表数据
            List<Xinwen> xinwenList=service.getListAll(obj);
            // 将结果存储到request中
            request.setAttribute("xinwenList", xinwenList);
            // 转发到JSP页面
            RequestDispatcher dispatcher = request.getRequestDispatcher("/showNewsList.jsp");
            dispatcher.forward(request, response);
        }else if(method.equals("list")){//列表
            Xinwen obj=new Xinwen();
            //获取列表数据
            List<Xinwen> xinwenList=service.getListAll(obj);
            // 将结果存储到request中
            request.setAttribute("xinwenList", xinwenList);
            // 转发到JSP页面
            RequestDispatcher dispatcher = request.getRequestDispatcher("/admin/manageNews.jsp");
            dispatcher.forward(request, response);
        }else if(method.equals("add")){//新增
            String title=request.getParameter("title");
            String content=request.getParameter("content");
            List<Object> objects=new ArrayList<>();
            objects.add(title);
            objects.add(content);
            service.add(objects);//新增
            response.sendRedirect("/XinwenServlet?method=list");
        }else if(method.equals("up")){//修改
            response.setContentType("text/json;charset=utf-8");
            String title=request.getParameter("title");
            String content=request.getParameter("content");
            String id=request.getParameter("id");
            List<Object> objects=new ArrayList<>();
            objects.add(title);
            objects.add(content);
            objects.add(id);
            service.up(objects);//修改
            response.sendRedirect("/XinwenServlet?method=list");
        }else if(method.equals("get")){//获取当前信息
            String id=request.getParameter("id");
            Xinwen xinwen=service.getId(id);
            // 将结果存储到request中
            request.setAttribute("xinwen", xinwen);
            // 转发到JSP页面
            RequestDispatcher dispatcher = request.getRequestDispatcher("/admin/manageNews.jsp");
            dispatcher.forward(request, response);
        }else if(method.equals("info")){//获取当前信息
            String id=request.getParameter("id");
            Xinwen xinwen=service.getId(id);
            // 将结果存储到request中
            request.setAttribute("xinwen", xinwen);
            // 转发到JSP页面
            RequestDispatcher dispatcher = request.getRequestDispatcher("/detailNews.jsp");
            dispatcher.forward(request, response);
        }else if(method.equals("del")){//删除
            String id=request.getParameter("id");
            List<Object> objects=new ArrayList<>();
            objects.add(id);
            service.del(objects);//删除
            response.sendRedirect("/XinwenServlet?method=list");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }


}
